<html>
<head>
	<meta charset='utf-8' />
	<title>小区信息助手</title>
	<meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0" />
	<style>
		
		@media screen and (max-width:450px){
			#content{width:98%;margin:0 auto;border:1px solid}
			/*页面宽度900以下时 .abc的宽度为450*/
		}
		@media screen and (min-width:450px){
			#content{width:500px;margin:0 auto;border:1px solid}
			/*页面宽度900以下时 .abc的宽度为450*/
		}
	</style>
</head>

<body id='content' style='position:relative'>

	<!--超管转让-->
	<div id='handOverCode' style='display:none;position:absolute;top:100px;height:250px;width:100%;background:#777;text-align:center;padding:20px 0'>
		<img src="../img/ewm.jpg" alt="" style='width:150px;height:150px'>	
		<div style='padding:10px 0'>
			受让人使用微信扫码，完成转让操作！
		</div>
		<div><button class='handOverBack'>返回</button></div>
	</div>

	<!--添加普管-->
	<div id='addAdminCode' style='display:none;position:absolute;top:100px;height:250px;width:100%;background:#777;text-align:center;padding:20px 0'>
		<img src="../img/ewm.jpg" alt="" style='width:150px;height:150px'>	
		<div style='padding:10px 0'>
			被提升权限人使用微信扫码，完成提权操作！
		</div>
		<div><button class='handOverBack'>返回</button></div>
	</div>
	
	<div style='height:100%;padding:5px' id='main' class='l1'>

		<div style='font-size:12px'>
			<div style='font-size:12px;float:right;display:none' id='ApplyLeader'>申请超管</div>
		</div>

		

		<h2 style='text-align:center'>管理员列表</h2>

		<div style='text-align:center;font-size:12px'>小区口令：<span>9350</span></div>

		<div id='user_list' style='font-size:13px'>
			<div style='line-height:50px;display:flex;margin:13px 0'>
				<div style='width:50px'><img src="../img/s9.jpg" alt="" style='width:50px;height:50px;border-radius:25px' /></div>
				<div style='flex:3;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;padding-left:5px'>果地佃世以</div>
				<div style='flex:1' id='handOver'>
					<span>转让</span>
					<span>罢免</span>
				</div>
				<div style='flex:1;text-align:right'>超管</div>
			</div>
			<div style='line-height:50px;display:flex;margin:13px 0'>
				<div style='width:50px'><img src="../img/s9.jpg" alt="" style='width:50px;height:50px;border-radius:25px' /></div>
				<div style='flex:3;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;padding-left:5px'>果地佃世以</div>
				<div style='flex:1' id='cancelAdmin'>正常</div>
				<div style='flex:1' id='cancelAdmin'>撤消</div>
				<div style='flex:1;text-align:right'>管理</div>
			</div>
		</div>
		
		<div style='text-align:center;margin:50px 0'>
			<button id='AddAdmin' style='display:none'>添加管理</button>
			<a href="area_detail.html"><button>返回</button></a>
		</div>

	</div>




</body>
</html>
<script src='../public/jquery.js'></script>
<script src='../public/fun.js'></script>
<script>
if(!sessionStorage.current_area_id){
	location.replace('area_detail.html');
}
var tokenSet = GetCityInfo();
var user_info = GetUserInfo();


//罢免超管
function _dismissAdmin(adminOpenid){
	var data = {
		type:'dismissAdmin',
		adminOpenid:adminOpenid,
		openid:user_info.openid,
		area_id:sessionStorage.current_area_id
	}
	AjaxBefore(data,function(res){
		if(!/^{.*}$/.test(res)){
			return;
		}
		var tmp = JSON.parse(res);
		sessionStorage.token1 = tmp.token1;
		switch(tmp.stat){
			case 'ok':A('申请已提交，等待投票！');break;
			case 'err1':case 'err3':A('操作异常！');break;
			case 'err2':A('不得重复申请！');break;
		}
	})
}

$(function(){
	//获取指定小区的管理员列表
		C('area_id：'+sessionStorage.current_area_id);
	GetAdminUserList(sessionStorage.current_area_id,function(data){
		
		//C(data);
		ParseUserList(data,user_info.openid);
	});

	//申请超管
	$('#ApplyLeader').click(function(){
		var data = {
			type:'ApplyLeader',
			area_id:sessionStorage.current_area_id
		};
		AjaxBefore(data,function(res){
			if(!/^{.*}$/.test(res)){
				return;
			}
			var tmp = JSON.parse(res);
			sessionStorage.token1 = tmp.token1;
			switch(tmp.stat){
				case 'err1':A('请求异常！');break;
				case 'err2':A('投票中，请等待结果！');break;
				case 'err3':A('请求失败!');break;
				case 'ok':A('申请已提交,等待投票结果！');break;
			}

		});
	});


	//添加 
	$('#AddAdmin').click(function(){
		$('#addAdminCode').show();
	});

		
	//修改小区口令
	$('#pwd_area_change').click(function(){
		$('#pwd_area').show();	
	});
	$('#pwd_area_back').click(function(){
		$('#pwd_area').hide();	
	});
});






//解析小区管理员列表
function ParseUserList(data,openid){
	if(typeof(data) != 'object' || data.length < 1){
		$('#ApplyLeader').show();	//超管不存在时显示申请超管
		$('#user_list').html('');
		return;
	}

	
	var is_leader = false;	//判断当前用户是否为超管
	var has_leader = false; //判断超管是否不存在
	for(var i=0;i<data.length;i++){
		if(data[i].level == 1 && data[i].openid == openid){
			is_leader = true;
		}
		if(data[i].level == 1){
			has_leader = true;
		}
	}

	if(!has_leader){
		$('#ApplyLeader').show();	//超管不存在时显示申请超管
	}

	var str = '';
	if(is_leader){	//我是超管
		//显示添加管理按钮
		$('#AddAdmin').show();
		for(var i=0;i<data.length;i++){
			if(data[i].level == 1){
				var level_name = '<span style="color:red">超管</span>';
				var con_name = '<span style="color:red" onclick=_transAdmin()>转让超管</span>';
				var con_type = 1; //转让操作
			}else{
				var level_name = '<span style="color:blue">管理</span>';
				var con_name = '<span  onclick=_cancelAdmin("'+data[i].openid+'")>撤消管理</span>';
				var con_type = 2; //撤消操作
			}
			str += "<div style='line-height:50px;display:flex;margin:13px 0'><div style='width:50px'><img src='../img/s9.jpg' alt='' style='width:50px;height:50px;border-radius:25px' /></div><div style='flex:3;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;padding-left:5px'>"+ data[i].openid +"</div><div style='flex:1'>"+con_name+"</div><div style='flex:1;text-align:right'>"+level_name+"</div></div>";
		}
	}else{		//我不是超管
		for(var i=0;i<data.length;i++){
			if(data[i].level == 1){
				var level_name = '<span style="color:red">超管</span>';
				var con_name = '<span onclick=_dismissAdmin("'+data[i].openid+'")>申请罢免</span>';
				var con_type = 3;
			}else{
				var level_name = '<span style="color:blue">管理</span>';
				var con_name = '';
				var con_type = 4;	//无用，占位
			}
			str += "<div style='line-height:50px;display:flex;margin:13px 0'><div style='width:50px'><img src='../img/s9.jpg' alt='' style='width:50px;height:50px;border-radius:25px' /></div><div style='flex:3;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;padding-left:5px'>"+ data[i].openid +"</div><div style='flex:1' >"+con_name+"</div><div style='flex:1;text-align:right'>"+level_name+"</div></div>";
		}
	}
	$('#user_list').html(str);
}






//撤消管理
function _cancelAdmin(adminOpenid){
	if(!adminOpenid){
		return;
	}
	var data = {
		type:'cancelAdmin',
		adminOpenid:adminOpenid,
		area_id:sessionStorage.current_area_id
	}
	AjaxBefore(data,function(res){
		if(!/^{.*}$/.test(res)){
			return;
		}
		var tmp = JSON.parse(res);
		sessionStorage.token1 = tmp .token1;
		if(tmp.stat == 'ok'){
			A('操作成功！');location.reload();
		}else{
			A('操作异常！');location.reload();
		}
	});
}


//转让超管
function _transAdmin(){
	var numb = prompt('序号？');
	if(!numb){
		retuen;
	}
	var data = {
		type:'transAdmin',
		area_id:sessionStorage.current_area_id,
		numb:numb
	}
	AjaxBefore(data,function(res){
		if(!/^{.*}$/.test(res)){
			return;
		}
		var tmp = JSON.parse(res);
		sessionStorage.token1 = tmp.token1;
		if(tmp.stat == 'ok'){
			A('操作成功！');
			location.reload();
		}else{
			A('操作异常！');
		}
	});
}


</script>